package com.itheima.dao;

import com.github.pagehelper.Page;
import com.itheima.pojo.Setmeal;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface SetmealDao {

    @Insert("insert into t_setmeal(id,name,code,helpCode,sex,age,price,remark,attention,img) values(#{id},#{name},#{code},#{helpCode},#{sex},#{age},#{price},#{remark},#{attention},#{img})")
    @SelectKey(statement = "select last_insert_id()", before = false, keyColumn = "id", keyProperty = "id", resultType = Integer.class)
    void add(Setmeal setmeal);

    //mybatis的注解开发，方法参数大于1，就要使用注解修饰别名@Param
    @Insert("insert into t_setmeal_checkgroup(setmeal_id, checkgroup_id) values(#{setmealId}, #{checkgroupId})")
    void setSetmealIdAndCheckGroupId(@Param("setmealId") Integer setmealId, @Param("checkgroupId") Integer checkgroupId);

    @Select("<script>   select * from  t_setmeal <if test=\"queryString!=null and queryString!=''\">where code=#{queryString} or name=#{queryString} or helpCode=#{queryString}</if>  </script>")
    Page<Setmeal> selectByCondition(@Param("queryString") String queryString);

    @Select("select * from t_setmeal")
    List<Setmeal> findAll();

    @Select("select * from t_setmeal where id=#{id}")
    @Results({
            @Result(property = "id", column = "id", id = true),
            @Result(property = "checkGroups",  column = "id", many = @Many(select = "com.itheima.dao.CheckGroupDao.findCheckGroupsBySetmealId"))
    })
    Setmeal findDetailById(Integer id);
    @Delete("delete from t_setmeal where id=#{id}")
    public void deleteById(Integer id);

    @Delete("delete from t_setmeal_checkgroup where setmeal_id=#{id}")
    public void deleteAssocication(Integer id);

    @Select("select s.name,count(o.id) value from t_order o,t_setmeal s where o.setmeal_id = s.id group by s.name")
    List<Map<String, Object>> findSetmealCount();
}
